package com.ks.core.config;

import com.baomidou.mybatisplus.mapper.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

@Component
public class MyMetaObjectHandler extends MetaObjectHandler {

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    /**
     * 插入时自动填充
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("createdDate", formatter.format(ZonedDateTime.now()), metaObject);
        this.setFieldValByName("lastUpdatedDate", formatter.format(ZonedDateTime.now()), metaObject);
    }

    /**
     * 更新时自动填充
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("lastUpdatedDate", formatter.format(ZonedDateTime.now()), metaObject);
    }
}
